package com.esheeper.mainserver.service;

import com.esheeper.mainserver.pojo.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserService {

    @Autowired
    MongoTemplate mongoTemplate;
    public UserDetail queryUserDetailById(String id){
        UserDetail userDetail = mongoTemplate.findById(id,UserDetail.class);
        if(userDetail == null){
            return createUserDetail(id);
        }
        return userDetail;
    }

    private UserDetail createUserDetail(String id){
        UserDetail userDetail = new UserDetail();
        userDetail.setId(id);
        userDetail.setSex(UserDetail.SEX_UNKNOWN);
        userDetail.setNickname(id.substring(0,8));
        userDetail.setCoinNumber(0);
        mongoTemplate.insert(userDetail);
        return userDetail;
    }
}
